﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Pex.Framework;
using Microsoft.Pex.Framework.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shweet.SPSolution.ShweetReader;
using Microsoft.SharePoint.Behaviors;

namespace Shweet.SPSolution.Tests.ShweetReader
{
    [TestClass]
    [PexClass(typeof(Parser))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
    [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
    public partial class ParserTests
    {
        [TestMethod]
        [HostType("Moles")]
        public void ConvertTagsToLinks_OneTagInMessage_OneTagIsConvertedToAnchor()
        {
            //State
            SetSPContextState();
            //Arrange
            string suppiledString = "#a a";
            string[] suppiledTags = new string[] {"#a"};
            
            //Act
            string convertedString = Parser.ConvertTagsToLinks(suppiledString, suppiledTags);
            //Assert
            Assert.AreEqual("<a href='http://b/b?tag=#a'>#a</a> a", convertedString);
        }

        private void SetSPContextState()
        {
            BSPContext bspContext = BSPContext.SetCurrent();
            BSPWeb web = bspContext.SetWeb();
            web.Url = "http://b/b";
        }

        [TestMethod]
        [HostType("Moles")]
        public void ConvertTagsToLinks_TwoTagsInMessage_OneTagIsConvertedToAnchor()
        {
            //State
            SetSPContextState();

            //Arrange
            string suppiledString = "#a #b";
            string[] suppiledTags = new string[] { "#a","#b" };

            //Acct
            string convertedString = Parser.ConvertTagsToLinks(suppiledString, suppiledTags);
            // Assert
            Assert.AreEqual("<a href='http://b/b?tag=#a'>#a</a> <a href='http://b/b?tag=#b'>#b</a>", convertedString);
        }

        [TestMethod]
        [HostType("Moles")]
        public void ConvertTagsToLinks_OneTagWithDifferentCasesInMessage_OneTagsIsConvertedToAnchor()
        {
            //State
            SetSPContextState();

            //Arrange
            string suppiledString = "#a";
            string[] suppiledTags = new string[] { "#A" };
            //Act
            string convertedString = Parser.ConvertTagsToLinks(suppiledString, suppiledTags);
            //Assert
            Assert.AreEqual("<a href='http://b/b?tag=#A'>#A</a>", convertedString);
        }

        [TestMethod]
        [HostType("Moles")]
        public void ConvertTagsToLinks_TwoTagsWithDifferentCasesInMessage_TwoTagsAreConvertedToAnchor()
        {
            //State
            SetSPContextState();
            //Arrange
            string suppiledString = "#a #b";
            string[] suppiledTags = new string[] { "#A", "#B" };
            //Act
            string convertedString = Parser.ConvertTagsToLinks(suppiledString, suppiledTags);
            //Assert
            Assert.AreEqual("<a href='http://b/b?tag=#A'>#A</a> <a href='http://b/b?tag=#B'>#B</a>", convertedString);
        }

        [TestMethod]
        [HostType("Moles")]
        public void ConvertTagsToLinks_TwoTagsRestOfTagsNullWithMessageInBetween_TwoTagsAreConvertedToAnchorsAndMessageLeftIntact()
        {
            //State
            SetSPContextState();
            //Arrange
            string suppiledString = "#a #b";
            string[] suppiledTags = new string[] {"#a", "#b", null, null};
            //Act
            string convertedString = Parser.ConvertTagsToLinks(suppiledString, suppiledTags);
            //Assert
            Assert.AreEqual("<a href='http://b/b?tag=#a'>#a</a> <a href='http://b/b?tag=#b'>#b</a>", convertedString);
        }

        // Not sure what value this gives us as the code is using regex in order to to it's work.
        [PexMethod]
        public string ConvertTagsToLinks(string suppiledString, string[] tags)
        {
            PexAssume.IsNotNull(suppiledString);
            PexAssume.IsNotNull(tags);
            PexAssume.AreElementsNotNull(tags);

            string result = Parser.ConvertTagsToLinks(suppiledString, tags);

            // think i might need round trip in here
            return result;
        }
    }
}
